package com.hjjt.baselib.util

import com.hjjt.baselib.util.ValueAnimatorUtil
import kotlin.Throws
import android.annotation.SuppressLint
import android.animation.ValueAnimator
import java.lang.Exception
import java.lang.reflect.Field

object ValueAnimatorUtil {
    private const val TAG = "ValueAnimatorUtil"

    /**
     * 如果动画被禁用，则重置动画缩放时长
     */
    fun resetDurationScaleIfDisable() {
        if (durationScale == 0f) resetDurationScale()
    }

    /**
     * 重置动画缩放时长
     */
    fun resetDurationScale() {
        try {
            field.setFloat(null, 1f)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val durationScale: Float
        private get() = try {
            ValueAnimatorUtil.field.getFloat(null)
        } catch (e: Exception) {
            e.printStackTrace()
            (-1).toFloat()
        }

    @get:Throws(NoSuchFieldException::class)
    private val field: Field
        private get() {
            @SuppressLint("SoonBlockedPrivateApi") val field =
                ValueAnimator::class.java.getDeclaredField("sDurationScale")
            field.isAccessible = true
            return field
        }
}